package com.yuan.balanceflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.balanceflow.constant.TransactionType;
import com.yuan.balanceflow.entity.UserWallet;
import com.yuan.balanceflow.entity.WalletTransactions;
import com.yuan.balanceflow.mapper.UserWalletMapper;
import com.yuan.balanceflow.mapper.WalletTransactionsMapper;
import com.yuan.balanceflow.service.IUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yuan_boss
 * @since 2023-03-17
 */
@Service
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements IUserWalletService {

    @Autowired
    private WalletTransactionsMapper walletTransactionsMapper;

    // 1.查询用户钱包余额
    @Override
    public BigDecimal getUserWalletBalance(Integer id) {
        UserWallet userWallet = baseMapper.selectById(id);
        BigDecimal balance = userWallet.getBalance();
        return balance;
    }


    //2.用户消费100元
    @Override
    public boolean userConsume(Integer id, BigDecimal amount) {
        //获取用户余额
        BigDecimal balance = baseMapper.selectById(id).getBalance();

        //余额不足
        if (balance.intValue() < amount.intValue()) {
            //抛出异常,全局异常捕获（省略）
            throw new RuntimeException("余额不足");
        }

        //计算余额
        balance = balance.subtract(amount);

        //更新用户余额
        boolean updateUserBalance = updateUserBalance(id, balance);

        //记录用户明细
        boolean res = false;
        if (updateUserBalance) {
            res = recordDetails(id, amount, balance, TransactionType.CONSUMER);
        }
        return res;
    }


    //退款
    @Override
    public boolean userDrawBack(Integer id, BigDecimal amount) {
        //获取用户余额
        BigDecimal balance = baseMapper.selectById(id).getBalance();

        //计算余额
        balance = balance.add(amount);

        //更新用户余额
        boolean updateUserBalance = updateUserBalance(id, balance);

        //记录用户明细
        boolean res = false;
        if (updateUserBalance) {
            res = recordDetails(id, amount, balance, TransactionType.DRAWBACK);
        }
        return res;
    }




    //更新用户余额
    private boolean updateUserBalance(Integer id, BigDecimal balance) {
        //设置更新用户余额的条件
        UpdateWrapper<UserWallet> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", id);

        UserWallet userWallet = new UserWallet();
        userWallet.setUserId(id);
        userWallet.setBalance(balance);
        int update = baseMapper.update(userWallet, updateWrapper);
        return update > 0;
    }

    //记录用户明细
    private boolean recordDetails(Integer id, BigDecimal amount, BigDecimal balance, String transactionType) {
        WalletTransactions walletTransactions = new WalletTransactions();
        walletTransactions.setUserId(id);
        walletTransactions.setTransactionType(transactionType);
        walletTransactions.setAmount(amount);
        walletTransactions.setBalance(balance);
        int insert = walletTransactionsMapper.insert(walletTransactions);
        return insert > 0;
    }

}
